'''
Created on May 8, 2012

@author: Vu Dao
'''
from selenium import webdriver
from Interface.contentPage import xpathCreateUnit, xpathTxtContentName,\
    xpathComboParentUnit, xpathComboContentType,\
    xpathComboxHideNavigationHandles, xpathComboUnitCompletionOptions,\
    xpathCheckBox, xpathInputText, xpathSubmitButton
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

# click on "Create Unit" button
def clickOnCreateUnitButton(self):
    browser = self.browser
    element = browser.find_element_by_xpath(xpathCreateUnit())
    element.click()
    
# add a new content
def addContent(self,contentName,content,parentUnitName="Without parent",contentType="Theory",hideNavigationHandlesType="No",unitCompletionOptions="Default",flag="No",edit="No"):
    browser = self.browser
    # input content name
    element = browser.find_element_by_xpath(xpathTxtContentName())
    element.clear()
    element.send_keys(contentName)
    # select Parent unit
    if edit == "No":
        element = browser.find_element_by_xpath(xpathComboParentUnit())
        Select(element).select_by_visible_text(parentUnitName)
    # Select content type
    element = browser.find_element_by_xpath(xpathComboContentType())
    Select(element).select_by_visible_text(contentType)
    # Select Hide navigation handles
    element = browser.find_element_by_xpath(xpathComboxHideNavigationHandles()) 
    Select(element).select_by_visible_text(hideNavigationHandlesType)
    # Select Unit completion options
    element = browser.find_element_by_xpath(xpathComboUnitCompletionOptions()) 
    Select(element).select_by_visible_text(unitCompletionOptions)
    # Select Toggle pdf/custom content
    
    if flag =="Yes":
        element = browser.find_element_by_xpath(xpathCheckBox()) 
        element.click()
    # input content of lesson
    browser.switch_to_frame("editor_content_data_ifr")
    element = browser.find_element_by_xpath(xpathInputText())
#    element.clear()
    element.send_keys(content)
    
    browser.switch_to_default_content()
    
# click on "Submit" button
def clickOnSubmitButton(self):
    browser = self.browser
    element = browser.find_element_by_xpath(xpathSubmitButton())
    element.click()
    
# verify add content of lesson successfully
def verifyAddContentSuccessfully(self,contentName,content):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//p[text()='"+contentName+"']")) 
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[contains(text(),'"+content+"')]"))
        return True
    except:
        return False
    
# click on sub content button
def clickOnSubContent(self):
#    browser = webdriver.Firefox()
    browser = self.browser
    element = browser.find_element_by_xpath("//a[text()='Create subunit']")
    element.click()
    
# click on update unit button
def clickOnUpdateUnit(self):
#    browser = webdriver.Firefox()
    browser = self.browser
    element = browser.find_element_by_xpath("//a[text()='Update unit']")
    element.click()
    
# click on Add comment button
def clickOnAddcomment(self):
    browser = self.browser
    element = browser.find_element_by_xpath("//a[text()='Add comment']")
    element.click()
    
    browser.switch_to_frame("popup_frame")
    
# add a new comment
def addANewComment(self,comment):
    browser = self.browser
#    browser = webdriver.Firefox()
#    browser.switch_to_frame("popup_frame")
    browser.switch_to_frame("data_ifr")
    element = browser.find_element_by_xpath("//p")
    element.send_keys(comment)
    
    
    
# click on Submit button
def clickOnSubmitCommentButton(self):
    browser = self.browser
    element = browser.find_element_by_xpath("//input[@name='submit']")
    element.click()
    browser.switch_to_default_content()
    
# verify create a new comment successfully
def verifyCreateANewComment(self,comment):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//p[text()='"+comment+"']"))
        return True
    except:
        return False
    
# delete a comment
def deleteComment(self,comment):
    browser = self.browser
#    browser = webdriver.Firefox()
    element = browser.find_element_by_xpath("//p[text()='"+comment+"']/../div/img")
    element.click()
    alert = browser.switch_to_alert()
    alert.accept()
    
    
